package cn.edu.uestc.service;

import cn.edu.uestc.pojo.Order;
import cn.edu.uestc.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StockService {
    @Autowired
    MongoTemplate mongoTemplate;

    public void updateStock(Product product) {
        mongoTemplate.insert(product, "products");
    }

    public List<String> getProductsByShopName(String shopName) {
        Query query = new Query(Criteria.where("shopName").is(shopName));
        List<Product> productList = mongoTemplate.find(query, Product.class, "products");
        List<String> productNames = new ArrayList<>();
        for (Product product : productList) {
            productNames.add(product.getProductName());
        }
        return productNames;
    }

    public List<Product> getAllProducts(String shopName) {
        Query query = new Query(Criteria.where("shopName").is(shopName));
        List<Product> productList = mongoTemplate.find(query, Product.class, "products");
        return productList;
    }

    public boolean deleteProductByName(String productName, String shopName) {
        Query query = new Query(Criteria.where("shopName").is(shopName).and("productName").is(productName));
        mongoTemplate.remove(query, Product.class,"products");
        return true;
    }
}
